package com.example.beikeyigou.search;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.beikeyigou.R;
import com.example.beikeyigou.goods.GoodsBean;
import com.example.beikeyigou.goods.GoodsDetailsActivity;
import com.example.beikeyigou.goods.GoodsTypeAdapter;
import com.example.beikeyigou.utils.BaseActivity;
import com.example.beikeyigou.utils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SearchActivity extends BaseActivity {
    private EditText searchEt;
    private Button searchBtn;
    private ListView goodsList;
    private ImageView backImg;

    GoodsTypeAdapter adapter;

    List<GoodsBean.DataBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        findView();
        initData();
        setListener();
        changeStatusBarTextImgColor(true);
    }

    public void changeStatusBarTextImgColor(boolean color) {
        if (color) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        } else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
        }
    }

    private void setListener() {
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchText;
                if (searchEt.getText().toString().equals("")) {
                    searchText = searchEt.getHint().toString();
                    searchEt.setText(searchText);
                    searchEt.setSelection(searchText.length());
                } else {
                    searchText = searchEt.getText().toString();
                }
                getNetwork(URLUtils.queryCommodityByName_url+searchText);
            }
        });

        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        goodsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(SearchActivity.this, GoodsDetailsActivity.class);
                intent.putExtra("id", data.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        GoodsBean goodsBean = new Gson().fromJson(result, GoodsBean.class);
        List<GoodsBean.DataBean> list = goodsBean.getData();
        data.clear();
        data.addAll(list);
        adapter.notifyDataSetChanged();
    }

    private void initData() {
        data = new ArrayList<>();
        adapter = new GoodsTypeAdapter(this, data);
        goodsList.setAdapter(adapter);
    }

    private void findView() {
        searchEt = findViewById(R.id.search_edit);
        searchBtn = findViewById(R.id.search_btn);
        goodsList = findViewById(R.id.search_listLv);
        backImg = findViewById(R.id.search_back);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}